/*
 * AdcValueTemperaturePlot.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   AdcValueTemperaturePlot.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   7 jan 2013
 */

#include <stdexcept>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>

#include "TemperatureScale.hpp"
#include "Thermistor.hpp"

#include "AdcValueTemperaturePlot.hpp"

using boost::shared_ptr;

namespace tltgen {

  AdcValueTemperaturePlot::AdcValueTemperaturePlot(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& solver, const shared_ptr<TemperatureScale const>& temperatureScale) :
    TemperaturePlot(thermistorTable, solver, temperatureScale)
  {
    setXBoundsAuto(false);
    setYBoundsAuto(false);
  }

  std::string AdcValueTemperaturePlot::getTitle() const
  {
    return "ADC - Temperature plot";
  }

  unsigned int AdcValueTemperaturePlot::getYMaxDigits() const
  {
    return 5;
  }
  std::string AdcValueTemperaturePlot::getYLabel() const
  {
    return "ADC";
  }
  tuple<PLFLT, PLFLT> AdcValueTemperaturePlot::getYBoundsAuto() const
  {
    return this->thermistorTable->getAdcValueBounds();
  }
  

  bool AdcValueTemperaturePlot::isValidRow(ThermistorTableRow* thermistorTableRow) const
  {
    return thermistorTableRow->hasValidTemperature() && thermistorTableRow->hasValidAdcValue();
  }
  PLFLT AdcValueTemperaturePlot::getYValue(ThermistorTableRow* thermistorTableRow) const
  {
    return thermistorTableRow->getAdcValue();
  }
  PLFLT AdcValueTemperaturePlot::getYValue(const shared_ptr<Thermistor>& thermistor, PLFLT xValue) const
  {
    Temperature kelvin_t = this->temperatureScale->toKelvin(xValue);
    Resistance r = thermistor->getResistance(kelvin_t);
    Voltage v = this->thermistorTable->getCircuit()->getOutputVoltage(r);
    AdcValue a = this->thermistorTable->getAnalogToDigitalConverter()->getOutputValue(v);
    return a;
  }
}
